#include "ubvm_sphere.h"
#include "DIVA_Manager.h"
#include <sstream>
#include "xParameterPack.h"
#include "vmLibrary.h"
#include "EntityMaterialInstance.h"
// 
//  name: ypc
//  history:  2009/3/10 
//            The original file x_cuboid_int_ptvm is copied to be the tempory body.

//Name           : hyt
//Update Date    : 08/10/23
//Update History :
//				(1) VL_createEntity 必須給 meshname,material可給可不給,若想自行命名vm的開頭,則可以
//                   再加第三個參數 string specialhead
//              (2) VL_regBoudingBox 則需要 vm的entity 
//              (3) 由programmer自行決定node內容
//              (4) 若要在vm任何一個位置顯示文字,首先呼叫VL_createVMText(你想顯示的文字,文字node的開頭name)
//                  再來是呼叫VL_createTextNode(vm文字位置)
//              (5) 設定vm node顏色則呼叫VL_setEntityColor(vm entity , Vector4(colorvalue))來設定

ubvm_sphere::ubvm_sphere(xParameterPack xpp):DIVA_VM(xpp){

	int radius_port = vmLibrary::instance().get_vm_default_input_parameters().size();
	_vm_radius = xpp.paras[radius_port].val_int_ptr ; // set up the private attributes

	init_default_parameters();
}
/// destructor
ubvm_sphere::~ubvm_sphere(){
}

void ubvm_sphere::load_3Dresources() {
	std::string meshName = "sphere.mesh";
	/// create entity and attach to the newly allocated scene node
	_ent = VL_createEntity(meshName,"TextureModColor");
	_SceneNode->attachObject(_ent);
	this->scaleBodyToAbsSize(100, 100, 100); 
}
void ubvm_sphere::set_default_values() {

	_vm_rx_val = 0 ;
	_vm_ry_val = 0 ;
	_vm_rz_val = 0 ;

	_vm_init_sizex = 100 ;
	_vm_init_sizey = 100 ;
	_vm_init_sizez = 100 ;

	_vm_sizex_val = 100 ;
	_vm_sizey_val = 100 ;
	_vm_sizez_val = 100 ;

	_vm_sizexscale_val = 1.0 ;
	_vm_sizeyscale_val = 1.0 ;
	_vm_sizezscale_val = 1.0 ;

	_vm_r_val = 0.2;
	_vm_g_val = 0.2;
	_vm_b_val = 0.2;
	_vm_transparency_val = 1.0 ;

	_vm_yaw_val = 0 ;
	_vm_pitch_val = 0 ;
	_vm_roll_val = 0 ;

	_vm_radius_val = 50 ;

}
void ubvm_sphere::reg_event_entity() {
	
	// register the relation of the entity and vm in diva manager
	// When this entity is clicked, mouse/keyboard VM will be passed to this VM
	// By default it can be only one entity representing this VM
	VL_regBoundingBox(_ent);

}

void ubvm_sphere::init_private_attrs_started() {
	int radius_port = vmLibrary::instance().get_vm_default_input_parameters().size();
	if(_vm_radius != NULL)  { // _vm_radius is either set by link or constants
		_vm_radius_val= (*_vm_radius);
		this->scaleBodyToAbsSize((_vm_radius_val) * 2, (_vm_radius_val)*2, (_vm_radius_val)*2);
		_prohibit_defaults[SKIP_SIZE] = true ; // we do not want the sphere to be resized again
		_prohibit_defaults[SKIP_SCALE] = true ;
	}
}

void ubvm_sphere::init_private_attrs_ended() {}

void ubvm_sphere::VMValueUpdated() {
	int radius_port = vmLibrary::instance().get_vm_default_input_parameters().size();
	if(_vm_radius != NULL)  { // _vm_radius is either set by link or constants
		_vm_radius_val= (*_vm_radius);
		this->scaleBodyToAbsSize((_vm_radius_val) * 2, (_vm_radius_val)*2, (_vm_radius_val)*2);
		_prohibit_defaults[SKIP_SIZE] = true ; // we do not want the sphere to be resized again
		_prohibit_defaults[SKIP_SCALE] = true ;
	}
	DIVA_VM::VMValueUpdated();
}

void ubvm_sphere::setBodyScale(Ogre::Vector3 scale) {
	_SceneNode ->setScale(scale);
}
bool ubvm_sphere::isBodyScaleWrtSceneNode() { return true ; }

Ogre::Vector3 ubvm_sphere::getOriginalBodySize() {
	Ogre::AxisAlignedBox temp = _ent ->getBoundingBox();	
	Ogre::Vector3 size = temp.getSize(); 
	return size ;
}

Ogre::Vector3 ubvm_sphere::getActualSize() {
	return getOriginalBodySize() * _SceneNode->getScale();
}
Ogre::Vector3 ubvm_sphere::getBodyScale() {
	return _SceneNode->getScale();
}